<?php
declare (strict_types = 1);

namespace app\controller\api;

use app\BaseController;
use app\service\QiniuService;
use app\service\LogService;

class Qiniu extends BaseController
{
    protected $qiniuService;
    protected $logService;

    protected function initialize()
    {
        parent::initialize();
        $this->qiniuService = new QiniuService();
        $this->logService = new LogService();
    }

    /**
     * 获取上传凭证
     */
    public function getUploadToken()
    {
        $adminId = session('admin_id');
        if (!$adminId) {
            $this->error('请先登录', null, 401);
        }

        $token = $this->qiniuService->getUploadToken();
        
        // 记录操作日志
        $this->logService->addLog($adminId, 'get_upload_token', '获取七牛云上传凭证');
        
        $this->success('获取成功', ['token' => $token]);
    }

    /**
     * 上传文件
     */
    public function upload()
    {
        $adminId = session('admin_id');
        if (!$adminId) {
            $this->error('请先登录', null, 401);
        }

        $file = request()->file('file');
        if (!$file) {
            $this->error('请选择文件');
        }

        $key = input('post.key');
        $result = $this->qiniuService->upload($file, $key);
        
        if ($result['code'] === 0) {
            // 记录操作日志
            $this->logService->addLog($adminId, 'upload_file', '上传文件：' . $result['data']['key']);
        }
        
        return json($result);
    }

    /**
     * 删除文件
     */
    public function delete()
    {
        $adminId = session('admin_id');
        if (!$adminId) {
            $this->error('请先登录', null, 401);
        }

        $key = input('post.key');
        if (empty($key)) {
            $this->error('参数错误');
        }

        $result = $this->qiniuService->delete($key);
        
        if ($result['code'] === 0) {
            // 记录操作日志
            $this->logService->addLog($adminId, 'delete_file', '删除文件：' . $key);
        }
        
        return json($result);
    }

    /**
     * 获取文件信息
     */
    public function getFileInfo()
    {
        $adminId = session('admin_id');
        if (!$adminId) {
            $this->error('请先登录', null, 401);
        }

        $key = input('get.key');
        if (empty($key)) {
            $this->error('参数错误');
        }

        $result = $this->qiniuService->getFileInfo($key);
        
        if ($result['code'] === 0) {
            // 记录操作日志
            $this->logService->addLog($adminId, 'get_file_info', '获取文件信息：' . $key);
        }
        
        return json($result);
    }

    /**
     * 获取文件列表
     */
    public function getFileList()
    {
        $adminId = session('admin_id');
        if (!$adminId) {
            $this->error('请先登录', null, 401);
        }

        $prefix = input('get.prefix', '');
        $marker = input('get.marker', '');
        $limit = input('get.limit/d', 1000);

        $result = $this->qiniuService->getFileList($prefix, $marker, $limit);
        
        if ($result['code'] === 0) {
            // 记录操作日志
            $this->logService->addLog($adminId, 'get_file_list', '获取文件列表');
        }
        
        return json($result);
    }

    /**
     * 获取文件访问URL
     */
    public function getFileUrl()
    {
        $adminId = session('admin_id');
        if (!$adminId) {
            $this->error('请先登录', null, 401);
        }

        $key = input('get.key');
        if (empty($key)) {
            $this->error('参数错误');
        }

        $url = $this->qiniuService->getFileUrl($key);
        
        // 记录操作日志
        $this->logService->addLog($adminId, 'get_file_url', '获取文件访问URL：' . $key);
        
        $this->success('获取成功', ['url' => $url]);
    }

    /**
     * 获取文件下载URL
     */
    public function getDownloadUrl()
    {
        $adminId = session('admin_id');
        if (!$adminId) {
            $this->error('请先登录', null, 401);
        }

        $key = input('get.key');
        if (empty($key)) {
            $this->error('参数错误');
        }

        $url = $this->qiniuService->getDownloadUrl($key);
        
        // 记录操作日志
        $this->logService->addLog($adminId, 'get_download_url', '获取文件下载URL：' . $key);
        
        $this->success('获取成功', ['url' => $url]);
    }
} 